package com.crazy.crypto.digest;

import com.crazy.crypto.common.CommonUtil;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Base64;
import java.util.Date;

public class Base64Util {

	/**
	 * encode via apache commons-codec
	 */
	public static String encode(String src) {
		return org.apache.commons.codec.binary.Base64.encodeBase64String(src.getBytes());
	}
	
	public static String encode(byte[] src) {
		return encode(new String(src));
	}
	
	public static String decode(String src) {
		return new String(org.apache.commons.codec.binary.Base64.decodeBase64(src));
	}

	/**
	 * encode via jdk utils
	 */
	public static String encode2(String src) {
		return Base64.getEncoder().encodeToString(src.getBytes());
	}

	public static String decode2(String src) {
		return new String(Base64.getDecoder().decode(src.getBytes()));
	}
	
	public static void main(String[] args) {
		String src = "www.tdw.cn";
		String encodedPutPolicy = "eyJzY29wZSI6Im15LWJ1Y2tldDpzdW5mbG93ZXIuanBnIiwiZGVhZGxpbmUiOjE0NTE0OTEyMDAsInJldHVybkJvZHkiOiJ7XCJuYW1lXCI6JChmbmFtZSksXCJzaXplXCI6JChmc2l6ZSksXCJ3XCI6JChpbWFnZUluZm8ud2lkdGgpLFwiaFwiOiQoaW1hZ2VJbmZvLmhlaWdodCksXCJoYXNoXCI6JChldGFnKX0ifQ==";
//		System.out.println(encode(src));
//		System.out.println(encode2(src));
//		// decode
//		System.out.println(decode(encode(src)));
//		System.out.println(decode2(encode2(src)));
//		String ss = "eyJzdWIiOiJ1c2VyIiwiaXNzIjoid3d3LnRkdy5jbiIsImV4cCI6MTUyMjE1MTA0NCwidXNlck5hbWUiOiIxODkyNzAzOTQyMHBjMTgwMzIwIiwidXNlcklkIjoiMzU2MUZCQzUtOEEwOC00Nzg0LTkzQzItRDMxMzdGRUFBN0Y5In0";
		System.out.println(decode(encodedPutPolicy));

		String sign = HmacSHA1Util.genHMAC(encodedPutPolicy,"MY_SECRET_KEY");
		System.out.println(sign);
//		long time = LocalDateTime.now().plusHours(1).toInstant(ZoneOffset.of("+8")).toEpochMilli();
//		System.out.println(time);
//		System.out.println(new Date(time));
	}
}
